<template>
    <div>
        <el-card class="box-card" shadow="never">
            <i class="el-icon-search"></i>
            <span>筛选搜索</span>
            <el-row :gutter="20" class="yx_el_row" style="margin-top:15px;">
                <el-col :span="4">
                    <el-button type="primary" plain @click="add">添加</el-button>
                </el-col>
            </el-row>
            <div class="yx_line"></div>
            <div style="margin-top:10px;">
              <i class="el-icon-tickets"></i>
              <span>数据列表</span>
            </div>
            <el-table class="yx_table" border :data="accountlist">
                <el-table-column type="index" label="序号" width="100">
                </el-table-column>
                <el-table-column prop="username" label="账号" width="180">
                </el-table-column>
                <el-table-column prop="realname" label="姓名" width="180">
                </el-table-column>
                <el-table-column prop="role_name" label="角色" width="100">
                </el-table-column>
                <el-table-column prop="created_at" label="创建时间" width="180">
                </el-table-column>
                <el-table-column prop="enabled" label="状态" width="100">
                </el-table-column>
                <el-table-column  label="操作">
                    <template slot-scope="scope">
                        <el-tooltip class="item" effect="dark" content="编辑" placement="bottom-start">
                            <el-button type="primary" icon="el-icon-edit" @click="edit(scope.row.id,2)" circle></el-button>
                        </el-tooltip>
                        <el-tooltip class="item" effect="dark" content="查看" placement="bottom-start">
                            <el-button type="info" icon="el-icon-notebook-1" @click="edit(scope.row.id,0)" circle></el-button>
                        </el-tooltip>
                        <el-tooltip class="item" effect="dark" content="删除" placement="bottom-start">
                            <el-button type="warning" icon="el-icon-delete" @click="del(scope.row.id)" circle></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <Pagination :total_count="total_count" :getList="getList"></Pagination>
        </el-card>
    </div>
</template>

<script>
    import { getAccountList,delAccount } from '@/api/account.js';
    import Pagination from '@/components/Pagination.vue';
    export default {
        name: 'AccountList',
        components:{ Pagination },
        inject:['reload'],
        data() {
            return {
                accountlist:[],
                total_count:0
            }
        },
        methods: {
            getList(current_page,page_size){
                this.$store.dispatch('loadCurrentPageSize',page_size);
                var send_data={
                    current_page:current_page,
                    page_size:page_size
                }
                let that=this;
                getAccountList(send_data).then(function(response){
                    if(response.data.code==1){
                      that.accountlist=response.data.list;
                      that.total_count=response.data.total_count;
                    }
                });
            },
            add(){
                this.$router.push({
                    name:'accountEdit',
                    params:{id:-1,edit_type:1}
                });
            },
            edit(id,edit_type){
                this.$router.push({
                    name:'accountEdit',
                    params:{id:id,edit_type:edit_type}
                });
            },
            del(id){
                let that=this;
                this.$confirm('确认删除, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then((e) => {
                    var send_data={
                        id:id,
                        is_delete:1
                    };
                    delAccount(send_data).then(function(response){
                        let type=response.data.code==1?'success':'error';
                        that.$message({
                            message:response.data.msg,
                            type:type,
                            onClose:function(e){
                                that.reload();
                            }
                        });
                    })
                })
            }
        },
        created() {
            this.getList(1,this.$store.state.default_page_size);
        },
    }
</script>
